﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ATMService
{
    public class ConnectDB
    {
        ManageATMDataContext mnATM;
        public ConnectDB()
        {
            mnATM = new ManageATMDataContext();
        }
        public Account GetAccount(string Ip)
        {
            Account acc = null;
            var query = from account in mnATM.Accounts
                        where account.Ip == Ip
                        select new
                        {
                            Ip = account.Ip,
                            Pin = account.Pin,
                            Name = account.Name,
                            TimeCreate = account.TimeCreate,
                            Balance = account.Balance
                        };
            foreach (var account in query)
            {
                acc = new Account
                {
                    Ip = account.Ip,
                    Pin = account.Pin,
                    Name = account.Name,
                    TimeCreate = account.TimeCreate,
                    Balance = Convert.ToInt32(account.Balance)
                };
            }

            return acc;
        }

        public bool ExceptMoney(int money, string Ip)
        {
            try
            {
                Account acc = mnATM.Accounts.Single(s => s.Ip == Ip);
                acc.Balance = acc.Balance - money;
                mnATM.SubmitChanges();
            }
            catch (Exception )
            {
                return false;
            }

            return true;
        }
        public bool AddMoney(int money, string Ip)
        {
            try
            {
                Account acc = mnATM.Accounts.Single(s => s.Ip == Ip);
                acc.Balance = acc.Balance + money;
                mnATM.SubmitChanges();
            }
            catch (Exception )
            {
                return false;
            }


            return true;
        }
        public bool SendingAccounts(Transaction trs)
        {
            Transaction sendingaccount = new Transaction
            {
                IpFrom = trs.IpFrom,
                IpTo = trs.IpTo,
                TypeTransaction = trs.TypeTransaction,
                TypeTransfer = trs.TypeTransfer,
                Amount = trs.Amount,
                Balance = trs.Balance,
                Description = trs.Description,
                time = trs.time
            };

            mnATM.Transactions.InsertOnSubmit(sendingaccount);
            mnATM.SubmitChanges();
            return true;
        }

        public List<Transaction> GetHistoryTransaction(string ip)
        {
            List<Transaction> lstHistory = new List<Transaction>();
            var query = (from trs in mnATM.Transactions
                        where trs.IpFrom == ip
                        orderby trs.time descending
                        select new
                        {
                            ID = trs.id,
                            IpFrom = trs.IpFrom,
                            IpTo = trs.IpTo,
                            TypeTransaction = trs.TypeTransaction,
                            TypeTransfer = trs.TypeTransfer,
                            Amount = trs.Amount,
                            Balance = trs.Balance,
                            Description = trs.Description,
                            time = trs.time
                        }).Take(10);
            foreach (var transaction in query)
            {
                Transaction t = new Transaction
                {
                    id = transaction.ID,
                    IpFrom = transaction.IpFrom,
                    IpTo = transaction.IpTo,
                    TypeTransaction = transaction.TypeTransaction,
                    TypeTransfer = transaction.TypeTransfer,
                    Amount = transaction.Amount,
                    Balance = transaction.Balance,
                    Description = transaction.Description,
                    time = transaction.time
                };
                lstHistory.Add(t);
            }
            return lstHistory;
        }

        public bool ChangePIN(string PIN, string Ip)
        {
            try
            {
                Account acc = mnATM.Accounts.Single(s => s.Ip == Ip);
                acc.Pin = PIN;
                mnATM.SubmitChanges();
            }
            catch (Exception )
            {
                return false;
            }

            return true;
        }
        public List<Transaction> SearchInformationWithdrawal(string Ip,string typeTransaction ,DateTime fromDate , DateTime toDate)
        {
            List<Transaction> lstHistory = new List<Transaction>();
            var query = (from trs in mnATM.Transactions
                         where trs.IpFrom == Ip && trs.TypeTransaction == typeTransaction && fromDate <= trs.time && toDate >= trs.time
                         orderby trs.time descending
                         select new
                         {
                             ID = trs.id,
                             IpFrom = trs.IpFrom,
                             IpTo = trs.IpTo,
                             TypeTransaction = trs.TypeTransaction,
                             TypeTransfer = trs.TypeTransfer,
                             Amount = trs.Amount,
                             Balance = trs.Balance,
                             Description = trs.Description,
                             time = trs.time
                         }).Take(10);
            foreach (var transaction in query)
            {
                Transaction t = new Transaction
                {
                    id = transaction.ID,
                    IpFrom = transaction.IpFrom,
                    IpTo = transaction.IpTo,
                    TypeTransaction = transaction.TypeTransaction,
                    TypeTransfer = transaction.TypeTransfer,
                    Amount = transaction.Amount,
                    Balance = transaction.Balance,
                    Description = transaction.Description,
                    time = transaction.time
                };
                lstHistory.Add(t);
            }
            return lstHistory;
        }

       
    }
}